#include <stdio.h>
#include <unistd.h>
#include "Defines.cpp"
#include "Server.cpp"
#include "ClientConnectsHandler.cpp"
#include "FileScanner.cpp"

int main(int argc, char *args[])
{
    for(int i=0;i<argc;i++)
    {
        PrintLine(args[i]);
    }
    if(argc >= 2)
    {
        RootPath = args[1];
    }

    uint16_t port = 80;
    if(argc >= 3)
    {
        port = std::stoi(args[2]);
    }
    Server server;
    ClientConnectsHandler clientConnectsHandler;
    if (server.InitServer(&port) == false)
    {
        PrintLine("初始化服务器失败");
        return 0;
    }
    PrintLine("初始化服务器成功, [%d]", port);
    if (server.Listen(10) == false)
    {
        PrintLine("服务端监听失败");
        return 0;
    }
    PrintLine("服务端监听成功");
    while (1)
    {
        Server::ClientData clientData;
        if (server.Accept(&clientData) == false)
        {
            PrintLine("创建客户端套接字失败");
            continue;
        }
        unsigned char *ip = (unsigned char *)&(clientData.ClientAddr.sin_addr.s_addr);
        PrintLine("创建客户端套接字成功, [%d], [%d.%d.%d.%d]", clientData.ClientSock, ip[0], ip[1], ip[2], ip[3]);
        clientConnectsHandler.AddClientConnect(clientData.ClientSock, clientData.ClientAddr);
    }
}